More Related Content Similar to computer notes - Data Structures - 12 (20) More from ecomputernotes (17) computer notes - Data Structures - 121. Class No.12 Data Structures http://ecomputernotes.com 9. Searching for Duplicates 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 http://ecomputernotes.com 10. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 http://ecomputernotes.com 11. Searching for Duplicates 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 http://ecomputernotes.com 12. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 http://ecomputernotes.com 13. Searching for Duplicates 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 http://ecomputernotes.com 14. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 http://ecomputernotes.com 15. Searching for Duplicates 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 http://ecomputernotes.com 16. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 http://ecomputernotes.com 17. Searching for Duplicates 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14 15 4 9 7 http://ecomputernotes.com 32. C++ Implementation #include <stdlib.h> template <class Object> class TreeNode { public: // constructors TreeNode() { this->object = NULL; this->left = this->right = NULL; }; TreeNode( Object* object ) { this->object = object; this->left = this->right = NULL; }; http://ecomputernotes.com 36. C++ Implementation #include <iostream> #include <stdlib.h> #include "TreeNode.cpp" int main(int argc, char *argv[]) { int x[] = { 14, 15, 4, 9, 7, 18, 3, 5, 16,4, 20, 17, 9, 14,5, -1}; TreeNode<int>* root = new TreeNode<int>(); root->setInfo( &x[0] ); for(int i=1; x[i] > 0; i++ ) { insert(root, &x[i] ); } } http://ecomputernotes.com 37. C++ Implementation void insert(TreeNode<int>* root, int* info) { TreeNode<int>* node = new TreeNode<int>(info); TreeNode<int> *p, *q; p = q = root; while( *info != *(p->getInfo()) && q != NULL ) { p = q; if( *info < *(p->getInfo()) ) q = p->getLeft(); else q = p->getRight(); } http://ecomputernotes.com 38. C++ Implementation if( *info == *(p->getInfo()) ){ cout << "attempt to insert duplicate: " << *info << endl; delete node; } else if( *info < *(p->getInfo()) ) p->setLeft( node ); else p->setRight( node ); } // end of insert http://ecomputernotes.com 39. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 40. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 41. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 42. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 43. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 44. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 45. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 46. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p q http://ecomputernotes.com 47. Trace of insert 17, 9, 14, 5 14 15 4 9 7 18 3 5 16 20 17 p p->setRight( node ); node http://ecomputernotes.com Editor's Notes End of lecture 11 End of lecture 12